home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / STANDALO / BELCH!_P / BELCH!.C < prev   
C/C++ Source or Header  |  1990-08-21  |  3KB  |  182 lines

  1. /* ###################################################################
  2.  
  3. Belch! by Andrew Welch
  4.  
  5. This INIT was written completely for the fun of it.  It simply randomly
  6. plays a sound sampled from the a loud belch asychronously.  I suppose
  7. it has little redeeming social value, but I wanted to freak my brother
  8. out a bit, and I thought others might like to play around as well.
  9.  
  10. Who says computers have to be boring?
  11.  
  12. It uses an Asynch sound library from "L Products" (on AOL) which he
  13. adapted from Larry Rosenstein of Apple.
  14.  
  15. ################################################################### */
  16.  
  17. #include <SoundMgr.h>
  18.  
  19. #define    JMP                0x4EF9
  20. #define    SystemTaskTrap    0xA9B4
  21. #define    RandRange        1000
  22.  
  23. extern    void            AInitSnd (void);
  24. extern    OSErr            ASndPlay (Handle,Boolean);
  25. extern    void            AStopSnd (Boolean);
  26. extern    Boolean            SndIsPlaying (void);
  27. extern    void            SndTask (void);
  28. extern    pascal    void    CallBack (SndChannelPtr,SndCommand*);
  29.  
  30.  
  31. /* ################################################################### */
  32.  
  33. /* This function returns a random number between 0 and range-1 */
  34.  
  35. int    Randomize(range)
  36. int    range;
  37.  
  38. {
  39. long    rawResult;
  40.     
  41. rawResult=Random();
  42. if (rawResult<0)
  43.     rawResult *=-1;
  44. return((rawResult*range)/32768);
  45. }
  46.  
  47. main()
  48. {
  49.     asm
  50.     {
  51.         lea        @savedG,A1
  52.         move.l    A0, (A1)
  53.         _RecoverHandle
  54.         move.l    A0,-(SP)
  55.         _DetachResource
  56.  
  57. /* Load in the sound resource */
  58.  
  59.         clr.l    -(SP)
  60.         move.l    #'snd ',-(SP)
  61.         move.w    #1,-(SP)
  62.         _GetResource
  63.         lea        @sndHandle,A0
  64.         move.l    (SP)+,(A0)
  65.  
  66.         move.l    @sndHandle,D0
  67.         tst.l    D0
  68.         beq        @errorOut
  69.  
  70. /* Now lock it down and detach it */
  71.  
  72.         move.l    @sndHandle,A0
  73.         _HNoPurge
  74.         move.l    @sndHandle,A0
  75.         _HLock
  76.  
  77.         move.l    @sndHandle,-(SP)
  78.         _DetachResource
  79.  
  80. /* Patch SystemTask */
  81.     
  82.         move     #SystemTaskTrap,D0
  83.         _GetTrapAddress    NEWTOOL
  84.     
  85.         lea        @origSysTask,A1
  86.         move    #JMP,(A1)+
  87.         move.l    A0,(A1)
  88.  
  89.         move     #SystemTaskTrap,D0
  90.         lea        @patchSysTask,A0
  91.         _SetTrapAddress    NEWTOOL
  92.  
  93. @errorOut
  94.  
  95.         rts
  96.         
  97.  
  98. /* ################################################################### */
  99.  
  100. /* THINK uses A4 to reference globals and statics */
  101.  
  102. @setupA4
  103.  
  104.         lea        @savedA4,A0
  105.         move.l    A4,(A0)
  106.         move.l    @savedG,A4
  107.         rts
  108.         
  109.         
  110. /* ################################################################### */
  111.  
  112. /* THINK uses A4 to reference globals and statics */
  113.  
  114. @restoreA4
  115.  
  116.         move.l    @savedA4,A4
  117.         rts
  118.         
  119.         
  120. /* ################################################################### */
  121.  
  122. /* Here is our patch to SystemTaskTrap */
  123.  
  124. @patchSysTask
  125.  
  126.         lea        @inited,A0
  127.         tst.w    (A0)
  128.         bne        @beenInited
  129.         move.w    #1,(A0)
  130.         
  131. /* The Asynch unit hasn't been initialize yet -- do it now */
  132.  
  133.         jsr        @setupA4
  134.         jsr        AInitSnd
  135.         jsr        @restoreA4
  136.  
  137. @beenInited
  138.  
  139. /* Call this every time through SystemTask to stop the sound if it is done */
  140.  
  141.         jsr        @setupA4
  142.         jsr        SndTask
  143.         jsr        @restoreA4
  144.  
  145.         jsr        @setupA4
  146.         jsr        SndTask
  147.         jsr        @restoreA4
  148.  
  149.         move.w    #RandRange,-(SP)
  150.         jsr        Randomize
  151.         add.l    #2,SP
  152.  
  153.         tst.w    D0
  154.         bne        @origSysTask
  155.         
  156. /* We should play the sound! */
  157.  
  158.         jsr        @setupA4
  159.         move.w    #1,-(SP)
  160.         move.l    @sndHandle,-(SP)
  161.         jsr        ASndPlay
  162.         add.l    #6,SP
  163.         jsr        @restoreA4
  164.  
  165. @origSysTask
  166.  
  167.         nop
  168.         nop
  169.         nop
  170.  
  171.  
  172. /* ################################################################### */
  173.  
  174. /* Static storage */
  175.  
  176. @sndHandle    dc.l    0            /* Handle to the sound we should play */
  177. @inited        dc.w    0            /* Has the sound unit been initialize? */
  178. @savedG        dc.l    0            /* PTR to our code */
  179. @savedA4    dc.l    0            /* Saved value of A4 */
  180. @sndFlag    dc.w    0            /* Is a sound playing? */
  181.     }
  182. }